<?php
/**
 *author:韩帅
 *createtime:2017/6/21 15:33
 *description:
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class UserShopBalance extends Model
{
    /**
     * 定义主键字段
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 定义表名，不含前缀
     *
     * @var string
     */
    protected $table = 'wl_user_shop_balance_log';

    /**
     * 定义拒绝保存的字段
     *
     * @var array
     */
    protected $guarded = ['id'];

    /**
     * 开启laravel默认的时间记录
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * 定义启用的状态
     */
    const STATUS_BEGIN = 1;

    /**
     * 定义禁用的状态
     */
    const STATUS_STOP = 0;

    /**
     * 定义已删除的状态
     */
    const ISDELETE_YES = 1;

    /**
     * 定义未删除的状态
     */
    const ISDELETE_NO = 0;

    /**
     * 定义充值状态
     */
    const TYPE_GET_PAY = 4;

    /**
     * 定义扣款状态
     */
    const TYPE_PAY = 5;

    /**
     * 定义收入状态
     */
    const ACTION_TYPE_IN = 2;

    /**
     * 定义支出状态
     */
    const ACTION_TYPE_GO = 1;


    /**
     * 定义银行卡提现类型
     */
    const TYPE_WIDTHDRAW = 0;


    /**
     * 定义状态
     */
    const STATUS_ONE    = 0;

    /**
     * 定义状态
     */
    const STATUS_TWO    = 1;

    /**
     * 定义状态
     */
    const STATUS_THREE  = 2;

    /**
     * 定义状态
     */
    const STATUS_FOUR   = 3;

    /**
     * @var array 订单类型
     */
    private $type       = ['0' => '银行卡提现','1' => '退货','2' => '订单返利','3' => '购买消费'];

    /**
     * 收入支出类型
     */
    private $action_type = ['0' => '其他','1' => '支出','2' => '收入'];

    /**
     * 订单状态
     */
    private $status     = ['0' => '待审核','1' => '审核中','2' => '审核通过','3' => '审核失败'];

    /**
     * 处理交易明细列表
     */
    public  function translate($data)
    {
        $newArr = [];
        $newArr['total'] = $data['total'];
        $newArr['per_page'] = $data['per_page'];
        $newArr['current_page'] = $data['current_page'];
        $newArr['last_page'] = $data['last_page'];

        foreach ($data['data'] as &$v) {

            if (empty($v['order_sn'])) {

                $v['order_sn'] = '';
            }

            if (empty($v['money'])) {

                $v['money'] = 0.00;
            } else {

                if ($v['action_type'] === self::ACTION_TYPE_GO) {
                    $v['money'] = '-' . number_format($v['money'], 2);
                }
                if ($v['action_type'] === self::ACTION_TYPE_IN) {
                    $v['money'] = '+' . number_format($v['money'], 2);
                }
            }

            if (!empty($v['type'])) {

                $v['type'] = $this->type[$v['type']];
            }

            if (!empty($v['action_type'])) {

                $v['action_type'] = $this->action_type[$v['action_type']];
            }

            $v['status'] = $this->status[intval($v['status'])];

            if (empty($v['note'])) {

                $v['note'] = '';
            }

            $newArr['balance_log'][] = ['order_sn' => $v['order_sn'], 'money' => $v['money'], 'type' => $v['type'],
                'action_type' => $v['action_type'], 'status' => $v['status'], 'created_at' => $v['created_at']
            ];
        }

        return $newArr;
    }

    /**
     * 定义系统提现和用户的关联模型
     */
    public function user ()
    {
        return $this->hasOne('App\Models\User','id','user_id')->select('id','real_name','balance','id_card','user_sex','user_age');
    }

}